package com.jzr.medical.db1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzr.common.util.U;
import com.jzr.medical.db1.dto.VitalSignDto;
import com.jzr.medical.db1.mapper.TPatientMapper;
import com.jzr.medical.db1.mapper.TVitalSignMapper;
import com.jzr.medical.db1.model.TPatient;
import com.jzr.medical.db1.model.TVitalSign;
import com.jzr.medical.db1.service.VitalSignService;
import com.jzr.medical.db1.vo.PatientBaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VitalSignServiceImpl extends ServiceImpl<TVitalSignMapper,TVitalSign> implements VitalSignService {

    @Autowired
    TPatientMapper patientMapper;

    @Override
    public List<TVitalSign> getVitalSignList(VitalSignDto record) {
        if(U.isBlank(record.getPatientCureNo())){
            return null;
        }

        QueryWrapper<TVitalSign> qw = new QueryWrapper<>();
        qw.eq(U.isNotBlank(record.getPatientCode()),TVitalSign.PatientCode_,record.getPatientCode())
                .eq(TVitalSign.PatientCureNo_,record.getPatientCureNo())
                .ge(U.isNotBlank(record.getOpDateFrom()),TVitalSign.OpDate_,record.getOpDateFrom());

        if(U.isNotBlank(record.getOpDateTo())) {
            qw.lt(TVitalSign.OpDate_, record.getOpDateTo().plusDays(1));
        }
        return list(qw);
    }

    @Override
    public List<PatientBaseVo> getPatentList(TPatient record) {
        return patientMapper.getPatentBaseList(record);
    }

    @Override
    public List<LocalDate> getOpDateList(VitalSignDto record) {
        QueryWrapper<TVitalSign> qw = new QueryWrapper<>();
        qw.select("DISTINCT op_date");
        this.listObjs(qw,t->t);
        List<LocalDate> list = this.list(qw).stream().map(t->t.getOpDate()).collect(Collectors.toList());
        return list;
    }
}
